//
//  BaseDappViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/3/14.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "BaseDappViewController.h"
#import "PCScatterViewController.h"
#import "XHSessionViewController.h"
#import "FoundModel.h"
#import "WZSwitch.h"
#import "ChatServer.h"
#import "DappToolView.h"
#import "XHFullPopContentScrollView.h"
#import "LoginViewController.h"
#import "ShareView.h"
#import "AppDelegate+UMengHelper.h"
#import "FoundServer.h"
@interface BaseDappViewController ()
@property (nonatomic,strong)DappInfo*dapp;
@property (nonatomic,strong)NSString*roomId;
@property (nonatomic,strong) WZSwitch*switchView;
@property (nonatomic,strong) XHFullPopContentScrollView *backScrollView;
@property (nonatomic,strong) ChatServer*chatServer;
@property (nonatomic,strong) FoundServer*foundServer;
@property (nonatomic,strong) UIButton*rightBtn;
@end

@implementation BaseDappViewController
- (instancetype)initWithDapp:(DappInfo*)dapp withRoomId:(NSString*)roomId{
    if (self == [super init]) {
        self.dapp = dapp;
        self.roomId = roomId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)setupUI{
    self.view.backgroundColor = UIColorHex(333333);
    self.backScrollView = [[XHFullPopContentScrollView alloc]initWithFrame:self.view.bounds];
    self.backScrollView.contentSize = CGSizeMake(2*kScreenWidth, 0);
    self.backScrollView.scrollEnabled = NO;
    self.fd_interactivePopDisabled = YES;
    [self.view addSubview:self.backScrollView];
    self.customNavigationBar.backgroundColor = [UIColor blackColor];
    self.leftBackImg = @"white_back";
    UIView *titleV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, AdaptedWidth(180), AdaptedWidth(35))];
    self.switchView = [[WZSwitch alloc]initWithFrame:titleV.bounds];
    self.switchView.backgroundColor = UIColorHex(#333333);
    [titleV addSubview:self.switchView];
    [self.switchView setLeftString:self.dapp.title];
    [self.switchView setRightString:XLocalized(@"聊天室")];
    [self.switchView setSelectColor:UIColorHex(#000000)];
    [self.switchView setUnselectColor:UIColorHex(#F0F3F6)];
    [self.switchView setMoveViewColor:UIColorHex(#FFCC03)];
    [self.switchView setTextFont:kBoldFont(17)];
    self.StatusBarStyle = UIStatusBarStyleLightContent;
    self.navigationItem.titleView = titleV;
    kWeakSelf(self);
    self.switchView.block = ^(BOOL state) {
        if (state) {
            [weakself.switchView setSwitchState:NO animation:YES];
            if (!kCurUser) {
                [XAlertTool alertTitle:XLocalized(@"检测到还未关联BeesID，是否前往关联？") message:XLocalized(@"创建即可使用聊天室及云收藏等功能！") cancelAction:^{
                    [weakself.switchView setSwitchState:NO animation:YES];
                } sureAction:^{
                    LoginViewController*login = [LoginViewController new];
                    login.CancelBlock = ^{
                        [weakself.switchView setSwitchState:NO animation:YES];
                    };
                    login.LoginSuccess = ^{
                        
                        if([[NIMSDK sharedSDK].teamManager isMyTeam:weakself.roomId]){
                            [weakself.switchView setSwitchState:YES animation:YES];
                            [weakself.backScrollView setContentOffset:CGPointMake(kScreenWidth, 0) animated:YES];
                            return;
                        }else{
                            //                    [XAlertTool alertTitle:[NSString stringWithFormat:@"%@?",XLocalized(@"加入群聊")] message:nil cancelAction:^{
                            //
                            //                    } sureAction:^{
                            [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
                            [weakself.chatServer joinChatroom:weakself.roomId completion:^(BOOL success, NSString * _Nonnull msg) {
                                [MBProgressHUD hideHUDForView:weakself.view];
                                if (success) {
                                    [weakself.switchView setSwitchState:YES animation:NO];
                                    [weakself.backScrollView setContentOffset:CGPointMake(kScreenWidth, 0) animated:YES];
                                }else{
                                    [weakself.view makeToast:msg];
                                }
                            }];
                            //                    }];
                        }
                    };
                    [weakself.navigationController hh_pushBackViewController:login];
                }];
                return;
            }else{
                if([[NIMSDK sharedSDK].teamManager isMyTeam:weakself.roomId]){
                    [weakself.switchView setSwitchState:YES animation:YES];
                    [weakself.backScrollView setContentOffset:CGPointMake(kScreenWidth, 0) animated:YES];        weakself.rightBtn.hidden = YES;
                    return;
                }else{
//                    [XAlertTool alertTitle:[NSString stringWithFormat:@"%@?",XLocalized(@"加入群聊")] message:nil cancelAction:^{
//
//                    } sureAction:^{
                        [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
                        [weakself.chatServer joinChatroom:weakself.roomId completion:^(BOOL success, NSString * _Nonnull msg) {
                            [MBProgressHUD hideHUDForView:weakself.view];
                            if (success) {
                                [weakself.switchView setSwitchState:YES animation:NO];
                                [weakself.backScrollView setContentOffset:CGPointMake(kScreenWidth, 0) animated:YES];
                                weakself.rightBtn.hidden = YES;
                            }else{
                                [weakself.view makeToast:msg];
                            }
                        }];
//                    }];
                }
            }
        }else{
            weakself.rightBtn.hidden = NO;
            [weakself.backScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }
    };
    
    PCScatterViewController*scatter = [PCScatterViewController new];
//    scatter.navigationBarHidden = YES;
    scatter.dappInfo = self.dapp;
    scatter.view.frame=CGRectMake(0, 0, kScreenWidth,self.backScrollView.height);
    scatter.customNavigationBar.backgroundColor = UIColor.clearColor;
    scatter.view.backgroundColor = UIColor.clearColor;
    [self addChildViewController:scatter];
    [self.backScrollView addSubview:scatter.view];
    
    XHSessionViewController *session = [[XHSessionViewController alloc]initWithSession:[NIMSession session:self.roomId type:NIMSessionTypeTeam]];
    session.view.frame =  CGRectMake(kScreenWidth, 0, kScreenWidth,self.backScrollView.height);
//    session.customNavigationBar.backgroundColor = UIColor.clearColor;
    session.view.backgroundColor = UIColor.clearColor;
    [self addChildViewController:session];
    [self.backScrollView addSubview:session.view];
    
    self.rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
    [self.rightBtn setImage:IMAGE_NAMED(@"tool_btn") forState:UIControlStateNormal];
    [self.rightBtn addTapBlock:^(UIButton * _Nonnull btn) {
        if (kCurUser) {
            weakself.foundServer.collectionId = weakself.dapp.dappId;
            [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
            [weakself.foundServer isCollectionCompletion:^(BOOL success, NSString * _Nonnull msg) {
                [MBProgressHUD hideHUDForView:weakself.view];
                [DappToolView showIsCollect:weakself.foundServer.isCol complet:^(ToolType type) {
                    switch (type) {
                        case ToolTypeRefresh:{
                            [scatter reload];
                        }
                            break;
                        case ToolTypeCopy:{
                            [PhoneTool copyStr:weakself.dapp.address];
                        }
                            break;
                        case ToolTypeShare:{
                            [ShareView showCallback:^(ShareType type) {
                                UMSocialPlatformType umSocialPlatformType = type+1;
                                NSString *title = weakself.dapp.title;
                                NSString *ps = weakself.dapp.introduction;
                                NSString *thumbImage = weakself.dapp.imgUrl;
                                NSString *url = weakself.dapp.address;
                                [kAppDelegate shareWebPageToPlatformType:umSocialPlatformType shareTitle:title subTitle:ps thumbImage:thumbImage shareURL:url completion:^(BOOL success) {
                                }];
                            }];
                        }
                            break;
                        default:
                        {
                            if (!kCurUser) {
                                [XAlertTool alertTitle:XLocalized(@"您尚未登录BEES账号，无法进行收藏操作,是否登录？") message:nil cancelAction:^{
                                    
                                } sureAction:^{
                                    LoginViewController*login = [LoginViewController new];
                                    [weakself.navigationController hh_pushBackViewController:login];
                                }];
                                return;
                            }
                            if (weakself.foundServer.isCol) {
                                weakself.foundServer.collectionId = weakself.dapp.dappId;
                                [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
                                [weakself.foundServer putCollectionListCompletion:^(BOOL success, NSString * _Nonnull msg) {
                                    [MBProgressHUD hideHUDForView:weakself.view];
                                    [weakself.view makeToast:msg];
                                }];
                            }else{
                                weakself.foundServer.collectionId = weakself.dapp.dappId;
                                [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
                                [weakself.foundServer  postCollectionListCompletion:^(BOOL success, NSString * _Nonnull msg) {
                                    [MBProgressHUD hideHUDForView:weakself.view];
                                    [weakself.view makeToast:msg];
                                }];
                            }
                        }
                            break;
                    }
                }];
            }];
            
        }else{
            [DappToolView showIsCollect:NO complet:^(ToolType type) {
                switch (type) {
                    case ToolTypeRefresh:{
                        [scatter reload];
                    }
                        break;
                    case ToolTypeCopy:{
                        [PhoneTool copyStr:weakself.dapp.address];
                    }
                        break;
                    case ToolTypeShare:{
                        [ShareView showCallback:^(ShareType type) {
                            UMSocialPlatformType umSocialPlatformType = type+1;
                            NSString *title = weakself.dapp.title;
                            NSString *ps = weakself.dapp.introduction;
                            NSString *thumbImage = weakself.dapp.imgUrl;
                            NSString *url = weakself.dapp.address;
                            [kAppDelegate shareWebPageToPlatformType:umSocialPlatformType shareTitle:title subTitle:ps thumbImage:thumbImage shareURL:url completion:^(BOOL success) {
                                //                                if (success) {
                                //                                    [kAppWindow makeToast:XLocalized(@"分享成功")];
                                //                                }else{
                                //                                    [kAppWindow makeToast:XLocalized(@"分享失败")];
                                //                                }
                            }];
                        }];
                    }
                        break;
                    default:
                    {
                        if (!kCurUser) {
                            [XAlertTool alertTitle:XLocalized(@"您尚未登录BEES账号，无法进行收藏操作,是否登录？") message:nil cancelAction:^{
                                
                            } sureAction:^{
                                LoginViewController*login = [LoginViewController new];
                                [weakself.navigationController hh_pushBackViewController:login];
                            }];
                            return;
                        }
                    }
                        break;
                }
            }];
        }
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.rightBtn];
}

#pragma - mark getter
- (ChatServer*)chatServer{
    if (!_chatServer) {
        _chatServer = [ChatServer new];
    }
    return _chatServer;
}
- (FoundServer*)foundServer{
    if (!_foundServer) {
        _foundServer = [FoundServer new];
    }
    return _foundServer;
}

- (void)dealloc{
//    self.tabBarController.tabBar.hidden = NO;
}
@end
